function Order(customerId) {
	this.customerId =  customerId;
	this.dateTime = new Date();
	this.items = [];
}

var OrderManager = (function () {

    function OrderManager() {}

    OrderManager.prototype.createOrder = function (customerId) {
        this.order = new Order(customerId);
    };

    OrderManager.prototype.addItem = function (item) {
        this.order.items.push(item);
    };

    OrderManager.prototype.sendOrder = function () {
        if (this.isValid(this.order)) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var response = JSON.parse(xhr.responseText);
                    handleResponse(response);
                }
            };
            xhr.open("POST", "/api/orders");
			  xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xhr.send(JSON.stringify(order));
        }
        else {
            handleError({ message: "Nieprawidłowe zamówienie!" });
        }
    };

    OrderManager.prototype.isValid = function (order) {
        return order.items.length > 0;
    };

    return OrderManager;
}());




function handleResponse(response) {
    console.log(JSON.stringify(response));
}

function handleError(error) {
    console.log(error.message);
}